/**
 * form混合
 * */
import { mapState, mapMutations } from 'vuex'
import bus from '@/mixins/bus';
export default {
    data () {
        return {
            formName: 'form', // 筛选表单名称啥
            grid: {
                xl: 8,
                lg: 8,
                md: 12,
                sm: 24,
                xs: 24
            },
            collapse: false, // 是否展开更多筛选条件
            formData: {}, // 表单提交数据
            formDataQ: {}, // 表单查询类型
            copyFormData: {}, // copy form data 此处必须写
            copyFormDataRules: {} // copy form rules 此处必须写
        }
    },
    computed: {
        ...mapState('admin/layout', ['isMobile']),
        ...mapState('admin/baseList', [
            'listData',
            'formItemData',
            'formItemDataRules',
            'searchFormData',
            'searchFormDataQ'
        ]),
        labelWidth () {
            return this.isMobile ? undefined : 120
        },
        labelPosition () {
            return this.isMobile ? 'top' : 'right'
        }
    },
    watch: {
        formItemData (n, o) {
            this.formInfoChange();
        }
    },
    created () {
        this.formInfoChange();
    },
    methods: {
        ...mapMutations('admin/baseList', [
            'setSearchFormData',
            'setSearchFormDataQ'
        ]),
        formInfoChange () {
            // copy 一份数据 方便清空表单数据
            let copyFormData = JSON.parse(JSON.stringify(this.formItemData))
            let copyFormDataRules = JSON.parse(JSON.stringify(this.formItemDataRules))
            // 传给子组件的数据
            this.copyFormData = copyFormData
            this.copyFormDataRules = copyFormDataRules
            // 动态添加formData 提交数据内容
            copyFormData.forEach(item => {
                const obj = item.data;
                let name = '';
                let val = '';
                let searchType = '';
                if (obj.type === 'inputRange' || obj.type === 'inputNumberRange') {
                    obj.valueArr.forEach(item => {
                        name = item.name
                        val = item.value
                        searchType = item.data.searchType ? item.data.searchType : 'Like'
                        this.$set(this.formData, name, val)
                        this.$set(this.formDataQ, name + '_q', searchType)
                    })
                } else if (obj.type === 'select' && obj.name === 'commitType') {
                    name = obj.name
                    val = obj.value
                    searchType = item.data.searchType ? item.data.searchType : 'IN'
                    this.$set(this.formData, name, val)
                    this.$set(this.formDataQ, name + '_q', searchType)
                } else {
                    name = obj.name
                    val = obj.value
                    searchType = item.data.searchType ? item.data.searchType : 'Like'
                    this.$set(this.formData, name, val)
                    this.$set(this.formDataQ, name + '_q', searchType)
                }
            })
            this.setSearchFormData(this.formData) // 存储搜索条件
            this.setSearchFormDataQ(this.formDataQ) // 存储搜索条件类型
        },
        // 表格列表筛选件提交
        handleSubmit (name) {
            bus.$emit('bus-reset', {});
            this.$refs[name].validate(valid => {
                if (valid) {
                    this.$Message.loading({
                        content: '正在获取数据!',
                        duration: 1
                    })
                } else {
                    this.$Message.error('部分筛选条件选择错误，请重新选择！')
                    return false
                }
                // pzt - 8-27  还可以
                // 查询  更新页码为1
                this.$set(this.$data, 'nowPage', 1)
                this.$emit('on-submit')
            })
        },
        // 表格导出提交
        handleSubmits (name) {
            this.$refs[name].validate(valid => {
                if (valid) {
                    this.$Message.loading({
                        content: '正在获取数据!',
                        duration: 1
                    })
                } else {
                    this.$Message.error('部分筛选条件选择错误，请重新选择！')
                    return false
                }

                this.$emit('on-submits')
            })
        },
        // 表格列表筛选件清空
        handleReset (name) {
            bus.$emit('bus-reset', {});

            let copyFormData = JSON.parse(JSON.stringify(this.formItemData))
            let copyFormDataRules = JSON.parse(JSON.stringify(this.formItemDataRules))
            this.$set(this.$data, 'copyFormData', copyFormData)
            this.$set(this.$data, 'copyFormDataRules', copyFormDataRules)

            this.$set(this.$data, 'nowPage', 1)
            this.$refs[name].resetFields();
            this.$emit('on-reset')
        },
        // 组件内数据改变
        handleChangeFormData (data) {
            this.$set(this.$data, 'formData', data)
            this.setSearchFormData(this.formData) // 存储搜索条件
        },
        // 展开搜索切换
        collapseChange (data) {
            this.collapse = data
        }
    }
}
